home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 366 < prev    next >
Encoding:
Text File  |  1996-08-06  |  4.6 KB  |  107 lines

  1. Path: engnews1.Eng.Sun.COM!taumet!clamage
  2. From: kanze@lts.sel.alcatel.de (James Kanze US/ESC 60/3/141 #40763)
  3. Newsgroups: comp.std.c++
  4. Subject: Re: basic_string& operator=(const basic_string&)
  5. Date: 8 Feb 1996 16:30:33 GMT
  6. Organization: SEL
  7. Sender: news@lts.sel.alcatel.de
  8. Approved: clamage@eng.sun.com (comp.std.c++)
  9. Message-ID: <KANZE.96Feb8172852@slsvewt.lts.sel.alcatel.de>
  10. References: <4f81p2$j2u@bmtlh10.bnr.ca>
  11. NNTP-Posting-Host: taumet.eng.sun.com
  12. Content-Type: text
  13. In-Reply-To: "john's message of 06 Feb 1996 10:28:03 PST
  14. Apparently-To: std-c++@ncar.ucar.edu
  15. Content-Length: 3853
  16. X-Lines: 83
  17. Originator: clamage@taumet
  18.  
  19. In article <4f81p2$j2u@bmtlh10.bnr.ca> "john (j.d.) hickin"
  20. <hickin@bnr.ca> writes:
  21.  
  22. |> The following was taken from a copy of the DWP:
  23.  
  24. |>   basic_string<charT,traits,Allocator>&
  25. |>       operator=(const basic_string<charT,traits,Allocator>& str);
  26.  
  27. |>   Effects:
  28. |>     If *this and str are not the same object, modifies *this such that:
  29.  
  30. |>                       Table 7--operator=(str) effects
  31.  
  32. |>              +-------------------------------------------------+
  33. |>              | Element                    Value                |
  34. |>              +-------------------------------------------------+
  35. |>              |data()       points at the first element  of  an |
  36. |>              |             allocated  copy  of  the  the array |
  37. |>              |             whose first element is  pointed  at |
  38. |>              |             by str.size()                       |
  39. |>              |size()       str.size()                          |
  40. |>              |capacity()   a value at least as large as size() |
  41. |>              +-------------------------------------------------+
  42. |>     If *this and str are the same object, the member has no effect.
  43. |>   Returns:
  44. |>     *this
  45.  
  46. |> Aside from a seeming typographical error in the Value column for Element data()
  47. |> (... by str.data() ?) the wording of this seems to rule out a counted pointer
  48. |> representation.
  49.  
  50. |> Is my interpretation correct?
  51.  
  52. Well, it says `allocated copy', not `newly allocated copy', so
  53. strictly speaking...:-).  Still, what you are saying would seem to be
  54. the intent.
  55.  
  56. Note that in any case, the basic template cannot use a countedd
  57. pointer representation, or only with extreme difficulty, because of
  58. the allocator arguments to the constructors.  (Different instances may
  59. be allocated from different memory pools.)  I believe, however, that
  60. the intent is that the implementation provide optimized
  61. specializations for the `standard' cases (char and wchar_t, default
  62. traits and allocator).  The standard guarantees that all instances of
  63. the default allocator are, in effect, the same; this provides the
  64. necessary loop-hole for reference counting with regards to the
  65. allocator.
  66.  
  67. In many ways, calling the allocator is a side issue (although it is an
  68. observable side effect when a user defined allocator is used); what is
  69. important is the validity of addresses obtained from c_str, data, and
  70. expressions like &s[ i ].  I feel that these are really separate
  71. issues, at least from a standards point of view.  (From the
  72. implementation point of view, of course, I cannot imagine a case where
  73. I reallocate but do not invalidate the addresses.  The reverse is
  74. *not* necessarily true, however.)
  75.  
  76. The current wording of the standard in fact guarantees nothing.  For
  77. example, consider the following:
  78.  
  79.     string              s( "abc" ) ;
  80.     s.reserve( 100 ) ;              //  should be enough
  81.     char const*         ps( s.data() ) ;
  82.     s.remove( 0 , 1 ) ;
  83.     assert( *ps == 'b' ) ;
  84.  
  85. Is the assert guaranteed.  According to the present wording, all that
  86. is guaranteed is that s.remove did not reallocate; I seen nothing in
  87. the draft standard which would preclude the implementation from simply
  88. playing tricks with the start pointer, however, leaving ps pointing to
  89. the `a' (that is no longer in the string).
  90.  
  91. I do not know whether this is intended or not.  Personally, I have no
  92. problem with the above being undefined, as long as it is explicitly
  93. stated that it is undefined (e.g.: all non-const functions may
  94. invalidate any previously returned pointers, whether reallocation
  95. occurs or not).
  96. -- 
  97. James Kanze         Tel.: (+33) 88 14 49 00        email: kanze@gabi-soft.fr
  98. GABI Software, Sarl., 8 rue des Francs-Bourgeois, F-67000 Strasbourg, France
  99. Conseils, Θtudes et rΘalisations en logiciel orientΘ objet --
  100.                 -- A la recherche d'une activitΘ dans une region francophone
  101.  
  102.  
  103. [ comp.std.c++ is moderated.  Submission address: std-c++@ncar.ucar.edu.
  104.   Contact address: std-c++-request@ncar.ucar.edu.  The moderation policy is
  105.   summarized in http://reality.sgi.com/employees/austern_mti/std-c++/policy.html
  106. ]
  107.